package com.hb.game.pk.ui.activity.game.tiyan;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hb.game.pk.R;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.beans.NullBean;
import com.hb.game.pk.beans.RoomSendHBBean;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.utils.StringUtils;
import com.hb.game.pk.utils.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SendRedActivity extends BaseActivity<NullBean> {

    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.tv_round)
    TextView tvRound;
    @BindView(R.id.tv_1)
    TextView tv1;
    @BindView(R.id.tv_2)
    TextView tv2;
    @BindView(R.id.tv_3)
    TextView tv3;
    @BindView(R.id.tv_4)
    TextView tv4;
    @BindView(R.id.tv_5)
    TextView tv5;
    @BindView(R.id.tv_6)
    TextView tv6;
    @BindView(R.id.tv_7)
    TextView tv7;
    @BindView(R.id.tv_8)
    TextView tv8;
    @BindView(R.id.tv_9)
    TextView tv9;
    @BindView(R.id.tv_0)
    TextView tv0;
    int click = 0;
    String room_id;
    String money;
    int max;
    int min;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_send_res;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        Intent intent = getIntent();
        room_id = intent.getStringExtra("room_id");
    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        Call<RoomSendHBBean> call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST_TY).roomlist_room(room_id);
        call.enqueue(new Callback<RoomSendHBBean>() {
            @Override
            public void onResponse(Call<RoomSendHBBean> call, Response<RoomSendHBBean> response) {
                RoomSendHBBean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                String conf_max = body.getData().getConf_max();
                String conf_min = body.getData().getConf_min();
                max = Integer.parseInt(conf_max) / 100;
                min = Integer.parseInt(conf_min) / 100;
                tvRound.setText(String.format(getResources().getString(R.string.send_round), min, max));
            }

            @Override
            public void onFailure(Call<RoomSendHBBean> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
            }
        });
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        llBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SendRedActivity.this.finish();
            }
        });
    }

    @OnClick({R.id.tv_1, R.id.tv_2, R.id.tv_3, R.id.tv_4, R.id.tv_5, R.id.tv_6, R.id.tv_7, R.id.tv_8, R.id.tv_9, R.id.tv_0})
    public void onViewClicked(View view) {
        money = etMoney.getText().toString();
        if (StringUtils.isEmpty(money)) {
            ToastUtils.showToast("请先输入金额");
            return;
        }
        float i = Float.parseFloat(money);
        if (i < min || i > max) {
            ToastUtils.showToast("请先输入正确金额");
            return;
        }
        switch (view.getId()) {
            case R.id.tv_1:
                click = 1;
                changeBg(tv1);
                break;
            case R.id.tv_2:
                click = 2;
                changeBg(tv2);
                break;
            case R.id.tv_3:
                click = 3;
                changeBg(tv3);
                break;
            case R.id.tv_4:
                click = 4;
                changeBg(tv4);
                break;
            case R.id.tv_5:
                click = 5;
                changeBg(tv5);
                break;
            case R.id.tv_6:
                click = 6;
                changeBg(tv6);
                break;
            case R.id.tv_7:
                click = 7;
                changeBg(tv7);
                break;
            case R.id.tv_8:
                click = 8;
                changeBg(tv8);
                break;
            case R.id.tv_9:
                click = 9;
                changeBg(tv9);
                break;
            case R.id.tv_0:
                click = 0;
                changeBg(tv0);
                break;
        }
    }

    public void changeBg(final TextView view) {
        view.setBackgroundResource(R.mipmap.ic_lei_select);
        view.setTextColor(getResources().getColor(R.color.c_ffffff));
        call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST_TY).sendHB(room_id, money, click + "");
        call.enqueue(new Callback<NullBean>() {
            @Override
            public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                NullBean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    view.setBackgroundResource(R.mipmap.ic_lei_normal);
                    view.setTextColor(getResources().getColor(R.color.c_000000));
                    return;
                }
                ToastUtils.showToast(body.getInfo());
                SendRedActivity.this.finish();
            }

            @Override
            public void onFailure(Call<NullBean> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
            }
        });
    }
}
